package benny.ApkposStore.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import benny.ApkposStore.db.ConfigDBHelper;
import benny.ApkposStore.db.DBHelper;
import benny.ApkposStore.model.Dish_class;
import benny.ApkposStore.model.Floor;
import benny.ApkposStore.model.Make_tbl;
import benny.ApkposStore.model.Room;
import benny.ApkposStore.model.View_dian;

public class ApkConfigdbUntils extends Activity {
	
	// 获取打印机IP列表
	public static HashMap<String, String> getprint_ipmap(Context cx) {

		SQLiteDatabase configdb = ConfigDBHelper.getReadableDatabase(cx,"shop_config.db");
		Cursor cursor;
		String sqlstr="select name,ip,kitchen_name from config_printers";
		cursor = configdb.rawQuery(sqlstr, null);
		System.out.println("config_printers:"+cursor.getCount());
		HashMap<String, String> data = new HashMap<String, String>();
        String key=null;
        String values=null;
        String values2=null;
		while (cursor.moveToNext()) {
			key=cursor.getString(cursor.getColumnIndex("name"));			
			values=cursor.getString(cursor.getColumnIndex("ip"));
			values2=cursor.getString(cursor.getColumnIndex("kitchen_name"));
			data.put(key, values);
			data.put(key+"N", values2);
		}
		cursor.close();
		configdb.close();
		configdb=null;
		return data;

	}
	// 获取总单打印机IP列表
	public static List<HashMap<String, String>> gettotalprint_map( Context cx) {
		
	    SQLiteDatabase configdb = ConfigDBHelper.getReadableDatabase(cx,"shop_config.db");
		Cursor cursor;
		String sqlstr="select name,ip,total_name,print_list from config_printers where IFNULL(print_list,'')<>'' ";
		cursor = configdb.rawQuery(sqlstr, null);
		System.out.println("config_printers:"+cursor.getCount());
		List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

		while (cursor.moveToNext()) {
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("name", cursor.getString(cursor.getColumnIndex("name")));
			map.put("ip", cursor.getString(cursor.getColumnIndex("ip")));
			map.put("total_name", cursor.getString(cursor.getColumnIndex("total_name")));
			map.put("print_list", cursor.getString(cursor.getColumnIndex("print_list")));
			list.add(map);
		}
		cursor.close();
		configdb.close();
		configdb=null;
		return list;
		
	}
	
	// 获取支付方式列表
	public static HashMap<String, String> getpay_map(Context cx) {
		
		SQLiteDatabase configdb = ConfigDBHelper.getReadableDatabase(cx,"shop_config.db");
		Cursor cursor;
		String sqlstr="select * from config_payment where is_valid=1 ";
		cursor = configdb.rawQuery(sqlstr, null);
		System.out.println("config_payment:"+cursor.getCount());
		HashMap<String, String> data = new HashMap<String, String>();
		String key=null;
		String values=null;
		String values2=null;
		String values3=null;
		String values4=null;
		while (cursor.moveToNext()) {
			key=cursor.getString(cursor.getColumnIndex("id"));			
			values=cursor.getString(cursor.getColumnIndex("description"));
			values2=cursor.getString(cursor.getColumnIndex("name"));
			values3=cursor.getString(cursor.getColumnIndex("mark"));
			values4=cursor.getString(cursor.getColumnIndex("exchange_rate"));
			data.put(key, values);
			data.put(key+"N", values2);
			data.put(key+"M", values3);
			data.put(key+"R", values4);
		}
		cursor.close();
		configdb.close();
		configdb=null;
		return data;
		
	}
	
	// 获取房台列表
	public static HashMap<String, String> getroommap(Context cx) {

		SQLiteDatabase	configdb = ConfigDBHelper.getReadableDatabase(cx,"shop_config.db");
		Cursor cursor;
		String sqlstr="select alias_id,name from config_room";
		cursor = configdb.rawQuery(sqlstr, null);
		System.out.println("config_room:"+cursor.getCount());
		HashMap<String, String> data = new HashMap<String, String>();
        String key=null;
        String values=null;
		while (cursor.moveToNext()) {
			key=cursor.getString(cursor.getColumnIndex("alias_id"));			
			values=cursor.getString(cursor.getColumnIndex("name"));
			data.put(key, values);
		}
		cursor.close();
		configdb.close();
		configdb=null;
		return data;

	}
	
	// 读取sqlite获取菜类信息

	public static  List<Dish_class> updateClasslist(Context cx,int flag) {
		SQLiteDatabase	configdb = ConfigDBHelper.getReadableDatabase(cx,"shop_config.db");
		String selection = null;
			if (flag == 0) {
				selection = "father_id=0";
			} else {
				selection = "father_id<>0";
			}

			Cursor cursor = configdb.query("config_dish_class",
					new String[] { "id, name,father_id, rank_id" }, selection,
					null, null, null, "rank_id");
			System.out.println(cursor.getCount());

			ArrayList<Dish_class> data = new ArrayList<Dish_class>();
			while (cursor.moveToNext()) {
				Dish_class dish_class = new Dish_class();
				dish_class.setName(cursor.getString(cursor.getColumnIndex("name")));
				dish_class.setId(cursor.getInt(cursor.getColumnIndex("id")));
				dish_class.setFather_id(cursor.getInt(cursor
						.getColumnIndex("father_id")));

				data.add(dish_class);
			}
			cursor.close();
			configdb.close();
			configdb=null;
			return data;

		}
	
	// 读取sqlite获取做法信息

	public static List<Make_tbl> updatemakelist(Context cx) {

		SQLiteDatabase configdb = ConfigDBHelper.getReadableDatabase(cx, "shop_config.db");

		Cursor cursor;

		cursor = configdb.rawQuery(
				"select * from config_cook_method order by rank_id", null);
		System.out.println(cursor.getCount());

		ArrayList<Make_tbl> data = new ArrayList<Make_tbl>();
		while (cursor.moveToNext()) {

			Make_tbl make_tbl = new Make_tbl();
			make_tbl.setCook_class(cursor.getInt(cursor
					.getColumnIndex("cook_class")));
			make_tbl.setId(cursor.getInt(cursor.getColumnIndex("id")));
			make_tbl.setIs_charge(cursor.getInt(cursor
					.getColumnIndex("is_charge")));
			make_tbl.setCook_price(cursor.getInt(cursor
					.getColumnIndex("cook_price")));
			make_tbl.setIs_show_kitchen(cursor.getInt(cursor
					.getColumnIndex("is_show_kitchen")));
			make_tbl.setNumber_statistic_class(cursor.getInt(cursor
					.getColumnIndex("number_statistic_class")));
			make_tbl.setRank_id(cursor.getInt(cursor.getColumnIndex("rank_id")));

			make_tbl.setCook_name(cursor.getString(cursor
					.getColumnIndex("cook_name")));
			make_tbl.setCook_unit(cursor.getString(cursor
					.getColumnIndex("cook_unit")));
			make_tbl.setRetrieval_code(cursor.getString(cursor
					.getColumnIndex("retrieval_code")));

			data.add(make_tbl);
		}
		cursor.close();
		configdb.close();
		configdb=null;
		return data;

	}
	
	// 读取sqlite获取做法信息

		public static List<Make_tbl> updatemakelistlimit( Context cx,int offset,int count) {
			SQLiteDatabase	configdb = ConfigDBHelper.getReadableDatabase(cx,"shop_config.db");
			Cursor cursor;

			cursor = configdb.rawQuery(
					"select * from config_cook_method  order by rank_id limit "+offset+","+count+"", null);
			System.out.println(cursor.getCount());

			ArrayList<Make_tbl> data = new ArrayList<Make_tbl>();
			while (cursor.moveToNext()) {

				Make_tbl make_tbl = new Make_tbl();
				make_tbl.setCook_class(cursor.getInt(cursor
						.getColumnIndex("cook_class")));
				make_tbl.setId(cursor.getInt(cursor.getColumnIndex("id")));
				make_tbl.setIs_charge(cursor.getInt(cursor
						.getColumnIndex("is_charge")));
				make_tbl.setCook_price(cursor.getInt(cursor
						.getColumnIndex("cook_price")));
				make_tbl.setIs_show_kitchen(cursor.getInt(cursor
						.getColumnIndex("is_show_kitchen")));
				make_tbl.setNumber_statistic_class(cursor.getInt(cursor
						.getColumnIndex("number_statistic_class")));
				make_tbl.setRank_id(cursor.getInt(cursor.getColumnIndex("rank_id")));

				make_tbl.setCook_name(cursor.getString(cursor
						.getColumnIndex("cook_name")));
				make_tbl.setCook_unit(cursor.getString(cursor
						.getColumnIndex("cook_unit")));
				make_tbl.setRetrieval_code(cursor.getString(cursor
						.getColumnIndex("retrieval_code")));

				data.add(make_tbl);
			}
			cursor.close();
			configdb.close();
			configdb=null;
			return data;

		}


	
	// 读取sqlite获取菜品信息
	public static  List<View_dian> updateViewDianList( Context cx,String type) {
		SQLiteDatabase	configdb = ConfigDBHelper.getReadableDatabase(cx,"shop_config.db");
			Cursor cursor;
			String sql = "";
			if (type.equals("1")) {
				
				sql=	"select q.*,price_type,cnt_price from (SELECT a.*,b.[unit_price],b.[unit],"
										+ " b.[inner_rank_id] FROM config_dish a,config_unit_relation b where a.[id]=b.[dish_id]"
										+ " ) q ,(select  dish_id,0 AS price_type, min(inner_rank_id)  AS inner_rank_id,"
										+ " COUNT(*) AS CNT_PRICE from config_unit_relation WHERE ifnull(price_type,2) IN (0,2)"
										+ " group by dish_id union select  dish_id,1 AS price_type, min(inner_rank_id)  AS "
										+ " inner_rank_id,COUNT(*) AS cnt_price from config_unit_relation WHERE ifnull(price_type,2)"
										+ " IN (1,2) group by dish_id ) r where q.[id]=r.[dish_id] and q.[inner_rank_id]="
										+ " r.[inner_rank_id] and price_type=1 order by rank_id";
			} else {
				sql="select q.*,price_type,cnt_price from (SELECT a.*,b.[unit_price],b.[unit],"
										+ " b.[inner_rank_id] FROM config_dish a,config_unit_relation b where a.[id]=b.[dish_id]"
										+ " ) q ,(select  dish_id,0 AS price_type, min(inner_rank_id)  AS inner_rank_id,"
										+ " COUNT(*) AS CNT_PRICE from config_unit_relation WHERE ifnull(price_type,2) IN (0,2)"
										+ " group by dish_id union select  dish_id,1 AS price_type, min(inner_rank_id)  AS "
										+ " inner_rank_id,COUNT(*) AS cnt_price from config_unit_relation WHERE ifnull(price_type,2)"
										+ " IN (1,2) group by dish_id ) r where q.[id]=r.[dish_id] and q.[inner_rank_id]="
										+ " r.[inner_rank_id] and price_type=0 order by rank_id";
			}

			cursor = configdb.rawQuery(sql, null);
			System.out.println(cursor.getCount());

			ArrayList<View_dian> data = new ArrayList<View_dian>();
			while (cursor.moveToNext()) {
				View_dian view_dian = new View_dian();
				view_dian.setSearch_name(cursor.getString(cursor
						.getColumnIndex("search_name")));
				view_dian.setDish_name(cursor.getString(cursor
						.getColumnIndex("dish_name")));
				view_dian.setPrinter_name(cursor.getString(cursor
						.getColumnIndex("printer_name")));
				view_dian.setUnit(cursor.getString(cursor.getColumnIndex("unit")));
				view_dian.setId(cursor.getInt(cursor.getColumnIndex("id")));
				view_dian.setDish_class_id(cursor.getInt(cursor
						.getColumnIndex("dish_class_id")));
				view_dian.setIs_print_item(cursor.getInt(cursor
						.getColumnIndex("is_print_item")));
				view_dian
						.setIs_mark(cursor.getInt(cursor.getColumnIndex("is_mark")));
				view_dian.setIs_set_dinner(cursor.getInt(cursor
						.getColumnIndex("is_set_dinner")));
				view_dian.setClass_statistics(cursor.getInt(cursor
						.getColumnIndex("class_statistics")));
				view_dian.setDigit_search_id(cursor.getInt(cursor
						.getColumnIndex("digit_search_id")));
				view_dian.setUnit_price(cursor.getInt(cursor
						.getColumnIndex("unit_price")));
				view_dian.setInner_rank_id(cursor.getInt(cursor
						.getColumnIndex("inner_rank_id")));
				view_dian.setPrice_type(cursor.getInt(cursor
						.getColumnIndex("price_type")));
				view_dian.setCnt_price(cursor.getInt(cursor
						.getColumnIndex("cnt_price")));
				view_dian
						.setRank_id(cursor.getInt(cursor.getColumnIndex("rank_id")));

				data.add(view_dian);
			}
			cursor.close();
			configdb.close();
			configdb=null;
			return data;

		}
	
	// 读取sqlite获取楼层信息
	public static List<Floor> updatefloorlist( Context cx) {
		SQLiteDatabase	configdb = ConfigDBHelper.getReadableDatabase(cx,"shop_config.db");
			Cursor cursor= configdb
					.query("config_floor",
							new String[] { "id, floor_name, rank_id" },
							null,
							null, null, null,"rank_id");
			System.out.println(cursor.getCount());
		
			
			ArrayList<Floor> data = new ArrayList<Floor>();
			while (cursor.moveToNext()) {
				Floor floor = new Floor();
				floor.setFloor_name(cursor.getString(cursor.getColumnIndex("floor_name")));
				floor.setId(cursor.getInt(cursor.getColumnIndex("id")));
				floor.setRank_id(cursor.getString(cursor.getColumnIndex("rank_id")));
				
				data.add(floor);
			}
			cursor.close();
			configdb.close();
			configdb=null;
			return data;

		}
		

	// 读取sqlite获取房台信息
	public static List<Room> updateroomlist(Context cx) {
		SQLiteDatabase	configdb = ConfigDBHelper.getReadableDatabase(cx,"shop_config.db");
		Cursor cursor= configdb
				.query("config_room",
						new String[] { "id, alias_id, name, floor_id, status, price_type, rank_id" },
						null,
						null, null, null,"rank_id");
		System.out.println(cursor.getCount());
		
		//rawQuery
		ArrayList<Room> data = new ArrayList<Room>();
		while (cursor.moveToNext()) {
			Room room = new Room();
			room.setId(cursor.getInt(cursor.getColumnIndex("id")));
			room.setAlias_id(cursor.getString(cursor.getColumnIndex("alias_id")));
			room.setName(cursor.getString(cursor.getColumnIndex("name")));
			room.setFloor_id(cursor.getInt(cursor.getColumnIndex("floor_id")));
			room.setStatus(cursor.getInt(cursor.getColumnIndex("status")));
			room.setPrice_type(cursor.getInt(cursor.getColumnIndex("price_type")));	
			data.add(room);
		}
		cursor.close();
		configdb.close();
		configdb=null;
		return data;
	}
	
	// 读取sqlite获取搭台信息
		public static List<HashMap<String, String>> updateroomflag(Context cx,String room_id) {
	
			SQLiteDatabase	configdb = 
					DBHelper.getReadableDatabase(cx);
			
			String sql = "select orderid,ifnull(table_extent,'') as table_extent,open_time,dish_total,room_state  from apkpos_orders_new where table_num="+room_id+"";
			Cursor cursor= configdb.rawQuery(sql, null);
			List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
			while (cursor.moveToNext()) {
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("flag", cursor.getString(cursor.getColumnIndex("table_extent")));				
				map.put("open_time", cursor.getString(cursor.getColumnIndex("open_time")));				
				map.put("dish_total", cursor.getString(cursor.getColumnIndex("dish_total")));				
				map.put("room_state", cursor.getString(cursor.getColumnIndex("room_state")));				
				map.put("orderid", cursor.getString(cursor.getColumnIndex("orderid")));				
				list.add(map);
			}
			cursor.close();
			configdb.close();
			configdb=null;
			return list;
		}
	
	

// 读取获取房台状态
public static HashMap<String, String> updateroomstate( Context cx) {
	SQLiteDatabase	configdb = 
			DBHelper.getReadableDatabase(cx);
	String sql = "select table_num,max(room_state) as room_state,count(*) as cnt,dish_total," +
			"open_time  from apkpos_orders_new  where room_state<>7 group by table_num ";
	Cursor cursor= configdb.rawQuery(sql, null);
	System.out.println(cursor.getCount());
	
	//rawQuery
	HashMap<String, String> data = new HashMap<String, String>();
	String key="";
	String state="";
	String cnt="";
	String dish_total="";
	String open_time="";
	while (cursor.moveToNext()) {
		 key = cursor.getString(cursor.getColumnIndex("table_num"));			
		 state = cursor.getString(cursor.getColumnIndex("room_state"));
		 cnt = cursor.getString(cursor.getColumnIndex("cnt"));
		 dish_total = cursor.getString(cursor.getColumnIndex("dish_total"));
		 open_time = cursor.getString(cursor.getColumnIndex("open_time"));
		 data.put(key, state);			
		 data.put(key+"N", cnt);					
		 data.put(key+"P", dish_total);					
		 data.put(key+"T", open_time);					
	}
	cursor.close();
	configdb.close();
	configdb=null;
	return data;
}
}
